﻿requirejs.config({
    paths: {
        "superagent": "../../node_modules/superagent/superagent",
        "page": "../../node_modules/page/page",
        "jquery": "../../node_modules/jquery/dist/jquery",
        "store": "../../node_modules/store/dist/store.legacy.min"
    }
});
define(function (require, exports, module) {
    var page = require('page');
    var request = require('superagent');
    var module = {};
    var store = require('store');
    module.LoadPage = function () {
        var self = this;
        page("*", load);
        function load(ctx, next) {
            if (self.previousPath === ctx.hash) {
                return;
            }
            if (!ctx.hash || ctx.hash.length == 0) {
                return;
            }
            self.previousPath = ctx.hash;

            undefScripts();
            $(document).trigger('pjax:start');
            $('#content').fadeOut(200);

            var path = ctx.hash.split('?');
            var url = '';
            if (path.length == 1) {
                url = "/template/" + ctx.hash + ".html";
            } else {
                url = "/template" + path[0] + ".html" + '?' + ctx.querystring;
            }
            window.setTimeout(function () {
                request.get(url)
                    .set('Accept', "text/html")
                    .end(function (err, res) {
                        if (err)
                            return;

                        if (res.ok) {
                            $('#content').html(res.text);
                            self.LoadScripts();
                        }
                        $('#content').fadeIn(200);
                        $(document).trigger('pjax:end');
                    });
            }, 200);
        };
        page.start();
    };
    module.LoadScripts = function () {
        var selector = $('script[page-script]');
        if (0 != selector.length) {
            var page_script = selector.attr("page-script");
            var self = this;
            require([page_script], function () {
                try {
                    if (window.Module && 'function' == typeof window.Module) {
                        self.Modules[page_script] = window.Module;
                        window.Module = null;
                    }

                    var Module = self.Modules[page_script];
                    if (null != Module) {
                        var childModule = new Module();
                        if ('function' == typeof childModule.init) {
                            var promise = childModule.init();
                            if (promise && 'function' == typeof promise.then) {
                                promise.then(function (value) {
                                    $(document)[0].dispatchEvent(new Event('render:end'));
                                });
                            }
                            else {
                                $(document)[0].dispatchEvent(new Event('render:end'));
                            }
                        }

                    }
                } catch (e) {
                    console.log(e);
                }
            });
        };
    };
    module.Init = function () {
        this.BeforeInit();

        this.AfterInit();
    };
    module.BeforeInit = function () {
        if (!store.get("AuthCode")) {
            window.location.href = "login.html";
        }

    }
    module.AfterInit = function () {
        this.LoadPage();
        this.LoadScripts();
        this.Modules = {};
    }
    module.Init();
});

function undefScripts() {
    var selector = $('script[page-script]');
    if (0 != selector.length) {
        var page_script = selector.attr("page-script");
        require.undef(page_script);
    }
};